import { defineStore } from "pinia";

import {
    getErrorDataApi,
    exportErrorDataApi,
    getNewErrorDataApi
} from '@/Api/Error'
import { downloadFileData } from "@/utils/downloadFile";
import EventBus from "@/utils/EventBus";

import { useCommon } from "./Common";
const CommonStore = useCommon()


export const useError = defineStore('Error', {
    state: (): ErrorState => {
        return {
            loading: false,
            searchForm: {
                TimeArr: [],
                serial: null,
                startData: '',
                endData: '',
                department: '',
                illArea: '',
                departmentWard: [],
                current: 1,
                pageNo: 1,
                pageSize: 20,
                showSizeChanger: true,
                total: 0,
                pageSizeOptions: ['20', '30', '50', '100'],
                showTotal: (total: number) => `共 ${total} 条`,
                onChange(pageNo: number, pageSize: number) {
                    this.current = pageNo
                    this.pageNo = pageNo
                    this.pageSize = pageSize
                }
            },
            exportForm: {
                TimeArr: [],
                startData: '',
                endData: '',
                department: '',
                illArea: ''
            },
            tableData: [],
            alarmDate: 0
        }
    },
    actions: {
        async getErrorData() {
            this.loading = true
            if (this.searchForm.TimeArr && this.searchForm.TimeArr.length) {
                this.searchForm.startData = this.searchForm.TimeArr[0]
                this.searchForm.endData = `${this.exportForm.TimeArr[1]} 23:59:59`
            } else {
                this.searchForm.startData = ''
                this.searchForm.endData = ''
            }


            if (this.searchForm.departmentWard != undefined && this.searchForm.departmentWard.length == 1) {
                this.searchForm.department = this.searchForm.departmentWard[0]
                this.searchForm.illArea = null
            } else if (this.searchForm.departmentWard != undefined && this.searchForm.departmentWard.length == 2) {
                this.searchForm.department = this.searchForm.departmentWard[0]
                this.searchForm.illArea = this.searchForm.departmentWard[1]
            } else {
                if (CommonStore.department != '检验科') {
                    this.searchForm.department = CommonStore.department
                    this.searchForm.illArea = CommonStore.illArea
                } else {
                    this.searchForm.department = ''
                    this.searchForm.illArea = ''
                }
            }

            try {
                const data = await getErrorDataApi(this.searchForm) as any
                if (data) {
                    this.tableData = data[0]
                    this.tableData.forEach((item, index) => {
                        item.index = (this.searchForm.pageNo! - 1) * this.searchForm.pageSize! + index + 1
                        item.departmentWard = item.illArea ? [item.department, item.illArea] : [item.department]
                    })
                    this.searchForm.total = data[1][0].total
                }
            } finally {
                this.loading = false
            }
        },
        async exportData() {
            if (this.exportForm.TimeArr.length) {
                this.exportForm.startData = this.exportForm.TimeArr[0]
                this.exportForm.endData = `${this.exportForm.TimeArr[1]} 23:59:59`
            }

            this.exportForm.department = CommonStore.department == '检验科' ? '' : CommonStore.department
            this.exportForm.illArea = CommonStore.illArea

            const data = await exportErrorDataApi(this.exportForm)
            if (data) {
                downloadFileData(data, '报警记录.xlsx')
            }
        },
        async getNewError() {
            if (CommonStore.department != '检验科') {
                this.searchForm.department = CommonStore.department
            } else {
                this.searchForm.department = ''
            }
            const data = await getNewErrorDataApi({}) as any
            // console.log(data);
            if (data) {
                const alarmDate = new Date(data.alarmData).getTime()
                const nowDate = new Date().getTime()
                if (alarmDate !== this.alarmDate && nowDate - alarmDate < 300000) {
                    this.alarmDate = alarmDate
                    EventBus.emit('ShowNotification', data)
                }
            }
        }
    }
})